//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package SaveServlet;

import com.lyq.bean.Student;
import com.lyq.bean.User;
import com.lyq.bean.service.Login_service;
import com.lyq.bean.service.S_Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(
        name = "Login_Servlet",
        urlPatterns = {"/Login_Servlet"}
)
public class Login_Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login_Servlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String Usersort = request.getParameter("UserSort");
        String Username = request.getParameter("username");
        String Pwd = request.getParameter("pwd");
        new User();
        User user = (new Login_service()).getLoginSql(Usersort, Username, Pwd);
        if (user != null) {
            // 登录成功时清除可能存在的错误消息
            request.getSession().removeAttribute("message");
            
            request.getSession().setAttribute("UNAME", Username);
            request.getSession().setAttribute("SORT", Usersort);
            if (Usersort.equals("管理员")) {
                request.getRequestDispatcher("/jsp/manager/M_Home.jsp").forward(request, response);
            } else if (Usersort.equals("学生")) {
                // 获取学生完整信息并存储到Session中
                List<Student> students = new S_Service().seek_S("学号", "精确查询", Username, new ArrayList<>());
                if (!students.isEmpty()) {
                    Student student = students.get(0);
                    request.getSession().setAttribute("student", student);
                }
                request.getRequestDispatcher("/jsp/student/s/S_Home.jsp").forward(request, response);
            } else if (Usersort.equals("教师")) {
                request.getRequestDispatcher("/jsp/teacher/t/T_Home.jsp").forward(request, response);
            }
        } else {
            request.getSession().setAttribute("message", "用户名或密码错误，请重写输入！");
            request.getSession().setAttribute("url", "Login.jsp");
            response.sendRedirect("Message_2.jsp");
        }

    }
}
